package com.miplus.ccrm.core.service.strategy;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.strategy.TriggerEditVo;
import com.miplus.ccrm.core.controller.vo.strategy.TriggerReqVo;
import com.miplus.ccrm.core.controller.vo.strategy.TriggerResVo;
import com.miplus.ccrm.core.pojo.CpCalllistTrigger;
import com.miplus.ccrm.core.mapper.CpCalllistTriggerMapper;
import com.miplus.ccrm.core.pojo.CpCalllistTriggerQuerylink;
import com.miplus.ccrm.core.pojo.MsgDefine;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author pc
* @description 针对表【cp_calllist_trigger】的数据库操作Service实现
* @createDate 2024-03-21 20:36:44
*/
@Service
public class CpCalllistTriggerServiceImpl extends MPJBaseServiceImpl<CpCalllistTriggerMapper, CpCalllistTrigger>
    implements CpCalllistTriggerService{

    @Resource
    private CpCalllistTriggerQuerylinkService cpCalllistTriggerQuerylinkService;

    @Override
    public IPage<TriggerResVo> triggerList(TriggerReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                TriggerResVo.class,
                new MPJLambdaWrapper<CpCalllistTrigger>()
                        .selectAs("fun_getnames('BUSINESS.TYPE',t.businesstype)",TriggerResVo::getBusinesstypedsc)
                        .selectAll(CpCalllistTrigger.class)
                        .like(JPubUtils.IsNotEmpty(paras.getName()),"name",paras.getName())
                        .eq(paras.getStatus()!=null,"status",paras.getStatus())
                        .orderByAsc("triggerid")
        );
    }

    @Override
    public CmdResVo triggerUpdate(String $usrid, TriggerEditVo paras)
    {
        CpCalllistTrigger def=new CpCalllistTrigger();
        BeanUtils.copyProperties(paras,def);
        if(JPubUtils.IsEmpty(paras.getBusinesstype())){
            def.setBusinesstype(null);
        }
        def.setMddt(new Date());
        def.setMdusr($usrid);
        int ret=baseMapper.updateById(def);

        cpCalllistTriggerQuerylinkService.saveCustRule(def.getTriggerid(),paras.getTsql());

        return new CmdResVo(ret,def.getTriggerid().toString());
    }

    @Override
    public CmdResVo triggerInsert(String $usrid,TriggerEditVo paras)
    {
        CpCalllistTrigger def=new CpCalllistTrigger();
        BeanUtils.copyProperties(paras,def);
        def.setCrdt(new Date());
        def.setCrusr($usrid);
        def.setMddt(new Date());
        def.setMdusr($usrid);
        int ret=baseMapper.insert(def);
        return new CmdResVo(ret,def.getTriggerid().toString());
    }

    @Override
    public CmdResVo triggerDelete(int id)
    {
        int ret=baseMapper.deleteById(id);
        cpCalllistTriggerQuerylinkService.getBaseMapper().delete(
                new QueryWrapper<CpCalllistTriggerQuerylink>().eq("triggerid",id)
        );
        return new CmdResVo(ret,Integer.toString(id));
    }

    @Override
    public List<LabelResVo> getTriggerList()
    {
        return selectJoinList(
                LabelResVo.class,
                new MPJLambdaWrapper<CpCalllistTrigger>()
                        .selectAs("triggerid",LabelResVo::getValue)
                        .selectAs("name",LabelResVo::getLabel)
                        .select("triggerid","name")
                        .eq("status",1)
        );
    }
}




